<template>
	<view class="container" :style="{paddingTop: `${getSystemInfo.navBarHeight}px`}">
		
		<navBar title="卡友新闻资讯" marginLeft="30%" />
		
		<view class="type-content" v-if="typeShow" :style="{top: `${getSystemInfo.navBarHeight}px`}">
			<u-tabs
				:list="typeList"
				lineColor="#EE0A24"
				lineWidth="80"
				@click="clickTypeList"
			></u-tabs>
		</view>
		
		<view class="msg-content" v-if="msgData.length" :style="{marginTop: `${ typeShow ? '64' : '0' }px`}">
			<view class="mc-item" v-for="item in msgData" :key="item.id" @click="handleMsgDetail(item.id)">
				<view class="mci-left">
					<view class="mcil-title">{{ item.articleTitle || ''}}</view>
					<view class="mcil-info">{{ item.briefIntroduction || ''}}</view>
					<view class="mcil-btm">
						<view class="mcilb-desc">
							<view class="mcilbr-icon">
								<image src="https://coach-vehicle-uat-1313990257.cos.ap-beijing.myqcloud.com/miniapp/care/ic_read.png" mode=""></image>
							</view>
							{{ item.viewQuantity || 0}}阅读
						</view>
						<view class="mcilb-desc">
							<view class="mcilbr-icon">
								<image src="https://coach-vehicle-uat-1313990257.cos.ap-beijing.myqcloud.com/miniapp/care/ic_time.png" mode=""></image>
							</view>
							{{ item.publishTime }}
						</view>
					</view>
				</view>
				<view class="mci-right">
					<image :src="item.coverImage" mode=""></image>
				</view>
			</view>
		</view>
		<view class="tabs-loadmore-text" v-if="showLoadMore">{{ loadMoreText }}</view>
		
		<view v-if="!msgData.length" class="empty">
		 <image src="@/static/empty.png"></image>
		 <text>无数据</text>
		</view>
		
	</view>
</template>

<script>
	import navBar from '@/components/navBar.vue'
	import { mapGetters } from 'vuex'
	
	export default {
		components: {
			navBar
		},
		data() {
			return {
				typeList: [],
				msgData: [],
				total: 0,
				loadMoreText: "加载中...",
				showLoadMore: false,
				formDatas: {
					pageNum: 1,
					pageSize: 10,
					categoryId: null
				},
			};
		},
		computed: {
			...mapGetters('global', ['getSystemInfo']),
			typeShow(){
				return this.typeList.length > 1
			},
		},
		onLoad(){
			this.getTypeListData()
		},
		methods: {
			handleReset(){
				this.msgData = []
				this.formDatas.pageNum = 1
				this.loadMoreText = "加载中...",
				this.showLoadMore = false
			},
			handleMsgDetail(id){
				uni.navigateTo({
				  url: `/carePack/index/articleDetail?id=${id}`
				})
			},
			clickTypeList(item){
				this.handleReset()
				if(this.formDatas.categoryId !== item.id){
					this.formDatas.categoryId = item.id
					this.getMsgListData()
				}
			},
			async getMsgListData(){
				uni.showLoading({
					title: '加载中...'
				})
				const res = await this.$request({
					method: 'POST',
				  url: '/marketing-center/article/app/list',
					data: {
						...this.formDatas
					}
				})
				this.msgData = this.msgData.concat(res.datas.data)
				this.total = res.datas.count
				uni.hideLoading()
				uni.stopPullDownRefresh();
			},
			async getTypeListData(){
				const res = await this.$request({
					method: 'GET',
				  url: '/marketing-center/article/category/app/list',
				})
				this.typeList = res.datas
				if(this.typeList.length){
					this.formDatas.categoryId = this.typeList[0].id
					this.getMsgListData()
				}
			},
		},
		// 需要上滑请求则放出来
		onReachBottom() {
			if (this.msgData.length >= this.total) {
				this.loadMoreText = "没有更多数据了!"
				return;
			}
			this.showLoadMore = true;
			this.formDatas.pageNum += 1
			this.getMsgListData()
		},
		onPullDownRefresh() {
			this.msgData = []
			this.formDatas.pageNum = 1
			this.getMsgListData();
		},
	}
</script>

<style lang="scss" scoped>
.container {
	height: 100%;
	width: 100%;
	.type-content {
		position: fixed;
		left: 0;
		width: 100%;
		z-index: 99;
		box-sizing: border-box;
		background-color: #f5f5f5;
		padding: 20rpx;
		/deep/ .u-tabs__wrapper__nav__line {
			margin-left: 5%;
		}
	}
	
	.msg-content {
		padding: 20rpx 20rpx 0;
		box-sizing: border-box;
		.mc-item {
			// &:not(:nth-last-child(1)){
				margin-bottom: 24rpx;
			// }
			box-sizing: border-box;
			padding: 20rpx;
			border-radius: 8rpx;
			display: flex;
			justify-content: space-between;
			background-color: #fff;
			.mci-left {
				display: flex;
				flex-direction: column;
				justify-content: space-between;
				.mcil-title {
					font-size: 16px;
					color: #333;
					text-shadow: 0 0 0.1px currentColor;
					overflow: hidden;
					text-overflow: ellipsis;
					display: -webkit-box;
					-webkit-box-orient: vertical;
					-webkit-line-clamp: 1;
				}
				.mcil-info {
					margin: 8rpx 0 16rpx;
					font-size: 13px;
					color: #666;
					overflow: hidden;
					text-overflow: ellipsis;
					display: -webkit-box;
					-webkit-box-orient: vertical;
					-webkit-line-clamp: 2;
				}
				.mcil-btm {
					font-size: 11px;
					color: #999;
					display: flex;
					justify-content: space-between;
					align-items: center;
					.mcilb-desc {
						display: flex;
						align-items: center;
						.mcilbr-icon {
							margin-right: 8rpx;
							image {
								width: 20rpx;
								height: 20rpx;
							}
						}
					}
				}
			}
			.mci-right {
				margin-left: 20rpx;
				image {
					width: 220rpx;
					height: 160rpx;
					border-radius: 8rpx;
				}
			}
		}
	}

	// 无内容
	.empty {
		width: 100%;
		height: 100%;
		padding-bottom: 40rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		background-color: #ffffff;
		image {
			width: 220px;
			height: 180px;
		 }
		text {
			font-size: 30rpx;
			color: rgba(0,0,0,0.45);
			line-height: 30rpx;
		}
	}
	
	// 加载中、
	.tabs-loadmore-text {
		text-align: center;
		padding: 10px;
	}

}
</style>
